package com.yang.dao;

import com.yang.entity.Orders;

import java.sql.*;
import java.util.*;

public class OrdersDao {
    //修改订单状态
    public int updataStatus(String id,int status){
        Connection conn = BaseDao.getConn();
        PreparedStatement ps=null;
        int sum=0;
        try {
            ps=conn.prepareStatement("update orders set status=? where id=?");
            ps.setInt(1,status);
            ps.setString(2,id);
           sum= ps.executeUpdate();
        }catch(Exception e) {
            e.printStackTrace();
         }finally {
          BaseDao.close(null,ps,conn);
     }
        return sum;
 }
    //根据用户查询
    public List<Orders> queryByUid(int uid){
        Connection conn= BaseDao.getConn();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps=conn.prepareStatement("select * from orders where userid=? order by creationtime desc");
            ps.setInt(1,uid);
            rs = ps.executeQuery();
            List list=new ArrayList();
            while(rs.next()){
                Orders o=new Orders();
                o.setAddress(rs.getString("Address"));
                o.setCreationtime(rs.getDate("Creationtime"));
                o.setId(rs.getString("Id"));
                o.setStatus(rs.getInt("Status"));
                o.setTel(rs.getString("Tel"));
                o.setUsername(rs.getString("Username"));
                o.setUserid(rs.getInt("Userid"));
                o.setTotalamount(rs.getDouble("Totalamount"));
                list.add(o);
            }
            return list;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.close(rs,ps,conn);
        }
        return null;
    }
    //查询全部
    public List<Orders> queryAll(int uid){
        Connection conn= BaseDao.getConn();
        PreparedStatement ps=null;
        ResultSet rs=null;
        List list=new ArrayList();
        try {
            ps=conn.prepareStatement("select * from orders where userid=?");
            ps.setInt(1,uid);
            rs = ps.executeQuery();
            while(rs.next()){
                Orders o=new Orders();
                o.setId(rs.getString("id"));
                o.setUsername(rs.getString("Username"));
                o.setAddress(rs.getString("Address"));
                o.setCreationtime(rs.getDate("Creationtime"));
                o.setTel(rs.getString("Tel"));
                o.setTotalamount(rs.getDouble("Totalamount"));
                o.setStatus(rs.getInt("Status"));
                o.setUserid(rs.getInt("Userid"));
                list.add(o);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.close(rs,ps,conn);
        }
        return list;
    }
    //删除订单
    public int deleteById(String id){
        Connection conn= BaseDao.getConn();
        PreparedStatement ps=null;
        int sum=0;
        try {
            ps=conn.prepareStatement("delete from orders where id=?");
            ps.setString(1,id);
            sum= ps.executeUpdate();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.close(null,ps,conn);
        }
        return sum;
    }
}
